//
//  LoveViewController.m
//  MusicPlayer
//
//  Created by 李欢 on 15/5/27.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LoveViewController.h"
#import "NovelDataBase.h"
#import "NovelModel.h"
#import "RankDetailTableViewCell.h"
#import "PlayMusicViewController.h"

@interface LoveViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView * loveView;

@property (nonatomic, assign) TableViewEditType isEdit;

@property (nonatomic, retain) NSArray *array;

@property (nonatomic, retain) NSMutableArray *Urlarray;

@end

@implementation LoveViewController

- (void)dealloc
{
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"最爱歌曲";
        self.array = [NSArray array];
        self.Urlarray = [NSMutableArray array];
        
        self.isEdit = TableViewEditTypeIsNomal;
        
        UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
        
        self.navigationItem.rightBarButtonItem = bar;
    }
    return self;
}

#pragma mark - 返回图片的事件
- (void)exitsAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *exits = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(exitsAction:)];
    self.navigationItem.leftBarButtonItem = exits;
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [imageView setImage:[UIImage imageNamed:@"back.jpg"]];
    [self.view addSubview:imageView];
    
    self.array = [NovelDataBase selectAll];
    
    
    [self loveTable];
    
}

-(void)loveTable;
{
    self.loveView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.loveView.rowHeight = 80;
    self.loveView.alpha = 0.5;
    self.loveView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.loveView.dataSource = self;
    self.loveView.delegate = self;
    [self.view addSubview:self.loveView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString * cellIdentify = @"cell";
    RankDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[RankDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
    }
    
    NovelModel *model = [self.array objectAtIndex:indexPath.row];
    cell.nameLabel.text = model.song_name;
    cell.songLabel.text = model.singer_name;
    
     [self.Urlarray addObject:model.songId];

    
    
    return cell;
}



- (void)barAction:(id)sender
{
    if (self.isEdit == TableViewEditTypeIsEditing) {
        self.navigationItem.rightBarButtonItem.title = @"编辑";
        [_loveView setEditing:NO animated:YES];
        self.isEdit = TableViewEditTypeIsNomal;
    }else if (self.isEdit == TableViewEditTypeIsNomal){
        self.navigationItem.rightBarButtonItem.title = @"完成";
        [_loveView setEditing:YES animated:YES];
        self.isEdit = TableViewEditTypeIsEditing;
    }
    
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPa
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 先删除数据
        
        NovelModel *model  = [_array objectAtIndex:indexPath.row];
        // 刷新界面
        [NovelDataBase deleteWithName:model.song_name];
        
        [_array removeObjectAtIndex:indexPath.row];
        [_loveView reloadData];}
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    NovelModel *model = [self.array objectAtIndex:indexPath.row];
    
    PlayMusicViewController *playMusic = [[PlayMusicViewController alloc] init];
    
    playMusic.songName = model.song_name;
    playMusic.singer = model.singer_name;
    playMusic.songUrl = model.songId;

    
    playMusic.songArray = self.Urlarray;
    playMusic.songListArray =  self.array;
    playMusic.num = indexPath.row;
    
    
    [self.navigationController pushViewController:playMusic animated:YES];
    [playMusic release];
    

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}







@end
